<?php

namespace App\Helper;

use App\Models\Mzy\ProjectType;
use App\Models\Inq\InqOrders;
use App\Toolkit\MiscToolkit;

class ConfigHelper
{
    // 默认就诊时间表
    const DOCTOR_SCHEDULE = [
        // "id" =>0,
        "doctor_id" =>0,
        "mon_am" =>0,
        "mon_pm" =>0,
        "tues_am" =>0,
        "tues_pm" =>0,
        "wed_am" =>0,
        "wed_pm" =>0,
        "thur_am" =>0,
        "thur_pm" =>0,
        "fri_am" =>0,
        "fri_pm" =>0,
        "sat_am" =>0,
        "sat_pm" =>0,
        "sun_am" =>0,
        "sun_pm" =>0
    ];

    // 默认用户头像
    const DEFAULT_USER_AVATAR = 'http://ptd-image.tcmlive.com/user.png';

    // 默认医生头像
    const DEFAULT_DOCTOR_AVATAR = 'http://ptd-image.tcmlive.com/doctor_default_image.png';

    // tcmmooc图片前缀
    const TCMMOOC_AVATAR_PREFIX = 'http://image.tcmmooc.com/';

    // inquiry图片前缀
    const INQUIRY_AVATAR_PREFIX = 'http://ptd-image.tcmlive.com/';

    // 获取年龄
    public static function get_age($start, $end)
    {
        $a = strtotime($start) - strtotime($end);
        $b = $a / (3600*24) / 365;

        $data = floor($b);
        return $data;
    }

    // 获取当前年龄
    public static function getCurrentAge($birthday)
    {
        $a = time() - strtotime($birthday);
        $b = $a / (3600*24) / 365;

        $data = floor($b);
        return $data;
    }

    // 获取验证码
    public static function get_code($length = 6)
    {
        $code = rand(0, 9);

        for ($i = 1; $i < $length; $i++) {
            $code = $code.rand(0, 9);
        }

        return $code;
    }

    // 根据七牛地址获取医生完整头像
    public static function getDoctorAvatar($avatar)
    {
        if(trim($avatar) == '')
        {
            return self::DEFAULT_DOCTOR_AVATAR;
        }
        elseif (substr($avatar, 0, 6) == 'public')
        {
            return str_replace("public://", self::TCMMOOC_AVATAR_PREFIX, $avatar);
        }

        return self::INQUIRY_AVATAR_PREFIX . $avatar;
    }

    // 获取医生职称
    public static function getDoctorJob($job)
    {
        switch ($job) {
            case 1:
                $data = '主任医师';
                break;
            case 2:
                $data = '副主任医师';
                break;
            case 3:
                $data = '主治医师';
                break;
            case 4:
                $data = '住院医师';
                break;
            case 5:
                $data = '助理医师';
                break;
            // case 9:
            //  $data = '其他';
            //  break;
            default:
                $data = '';
                break;
        }
        return $data;
    }

    // 获取隐藏手机号
    public static function getHideMobile($mobile)
    {
        return substr($mobile,0,3) . ' **** ' . substr($mobile,7,4);
    }

    // 获取隐藏姓名
    public static function getHideName($name)
    {
        return mb_substr($name, 0, 1, 'utf-8') . str_repeat('*', mb_strlen($name, 'utf-8') - 1);
    }

    //获取距当前时间的时长显示
    public static function timeAgo($posttime){
        //当前时间的时间戳
        $nowtimes = strtotime(date('Y-m-d H:i:s'),time());
        //之前时间参数的时间戳
        $posttimes = strtotime($posttime);
        //相差时间戳
        $counttime = $nowtimes - $posttimes;

        //进行时间转换
        if($counttime<60){

            return$counttime.'秒前';

        }else if($counttime>60 && $counttime<3600){

            return intval(($counttime/60)).'分钟前';

        }else if($counttime>=3600 && $counttime<3600*24){

            return intval(($counttime/3600)).'小时前';

        }else if($counttime>=3600*24 && $counttime<3600*24*2){

            return '昨天';

        }else if($counttime>=3600*24*2 && $counttime<3600*24*3){

            return '前天';

        }else if($counttime>=3600*24*3 && $counttime<=3600*24*20){

            return intval(($counttime/(3600*24))).'天前';

        }else{

            return $posttime;

        }
    }

    // 获取时间差，返回天数
    public static function getTimeDiff($time)
    {
        $current_time = strtotime (date("Y-m-d H:i:s"));
        $old_time = strtotime ($time);
        return ($current_time-$old_time)/86400; //60s*60min*24h
    }

    // 月份补充
    public static function getMonth($month)
    {
        if(strlen($month) == 1)
        {
            return '0' . $month;
        }
        else
        {
            return $month;
        }
    }

    // 获取费用名称
    public static function getCostType($name, $type)
    {
        switch ($type) {
            case InqOrders::RECIPE_ORDER:
                $data = $name . '-诊后服务费';
                break;
            case InqOrders::INQUIRY_ORDER:
                $data = $name . '-问诊费';
                break;
            case InqOrders::REWARD_ORDER:
                $data = '打赏';
                break;
            case InqOrders::EXTRA_SERVICE_ORDER:
                $data = '诊金';
                break;
            case InqOrders::EXPERT_INQUIRY_ORDER:
                $data = '求助费';
                break;
            default:
                $data = '';
                break;
        }
        return $data;
    }

    // 获取当月第一天和最后一天
    public static function getCurrentMonth()
    {
        $date = date("Y-m-d H:i:s");
        $firstday = date('Y-m-01 00:00:00', strtotime($date));
        $lastday = date('Y-m-d 23:59:59', strtotime("$firstday +1 month -1 day"));
        return [$firstday, $lastday];
    }

    /**
     * 生成基础订单
     *
     * @param $doctor_id
     * @param $user_id
     * @param $expert_id
     * @param $team_id
     * @param $team_member_id
     *
     * @return string
     */
    public static function createBaseOrderNo($doctor_id, $user_id, $expert_id, $team_id, $team_member_id)
    {
        return MiscToolkit::uuid(microtime() . "_" . $user_id . "_" . $doctor_id . "_" . $expert_id . "_" . $team_id . "_" . $team_member_id);
    }

    /**
     * 生成订单号
     * 10 recipe_order药品订单
     * 20 inquiry_order问诊订单
     * 30 reward_order打赏订单
     * 40 expert_inquiry_order求助订单
     *
     * @param $type
     *
     * @return string
     */
    public static function createOrderNo($type, $length = 5)
    {
        if($type == 'recipe')
        {
            $code = '10';
        }
        elseif($type == 'inquiry')
        {
            $code = '20';
        }
        elseif($type == 'revisit')
        {
            $code = '30';
        }
        elseif($type == 'medical_service')
        {
            $code = '40';
        }
        elseif($type == 'team_inquiry')
        {
            $code = '50';
        }
        elseif($type == 'team_recipe')
        {
            $code = '60';
        }
        elseif($type == 'outpatient')
        {
            $code = '70';
        }

        $data = $code . date('YmdHis', time()) . self::get_code($length);

        return $data;
    }

    // 获取费用名称 1扫码2登记3问诊4开方5购药6评价7随访
    public static function getLastStatus($type)
    {
        switch ($type) {
            case 1:
                $data = '扫码';
                break;
            case 2:
                $data = '问诊';
                break;
            case 3:
                $data = '登记';
                break;
            case 4:
                $data = '开方';
                break;
            case 5:
                $data = '购药';
                break;
            case 6:
                $data = '评价';
                break;
            case 7:
                $data = '随访';
                break;
            default:
                $data = '';
                break;
        }
        return $data;
    }

    // 获取验证码
    public static function getSmsCode($length = 6)
    {
        $code = rand(0, 9);

        for ($i = 1; $i < $length; $i++) {
            $code = $code.rand(0, 9);
        }

        return $code;
    }

    // 高亮字段
    public static function setHighlightingField($key, $data)
    {
        return str_replace($key, "<matched>" . $key . "</matched>", $data);
    }

    // 经纬度计算距离
    // lat纬度，lng经度
    public static function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $lat1 = (float)$lat1;
        $lng1 = (float)$lng1;
        $lat2 = (float)$lat2;
        $lng2 = (float)$lng2;

        $earthRadius = 6367000; //approximate radius of earth in meters
        $lat1 = ($lat1 * pi() ) / 180;
        $lng1 = ($lng1 * pi() ) / 180;
        $lat2 = ($lat2 * pi() ) / 180;
        $lng2 = ($lng2 * pi() ) / 180;
        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;
        $distance = $calculatedDistance / 1000; //米换算千米
        return round($distance, 2);
    }

    // 获取年数(几年)
    public static function getStudyYear($time)
    {
        $currentDate = date("Y");

        $time = substr($time, 0, 4);

        return (int)$currentDate - (int)$time;
    }

    // 距离排序
    public static function getDistanceAsc($arr)
    {
        $count = count($arr);
        if ($count <= 1)
        {
            return $arr;
        }

        $temp = 0;
        for ($i=0; $i < $count-1; $i++)
        {
            for ($j=0; $j < $count-1-$i; $j++)
            {
                if ($arr[$j]->distance > $arr[$j+1]->distance)
                {
                    $temp         = $arr[$j];
                    $arr[$j]     = $arr[$j+1];
                    $arr[$j+1]     = $temp;
                }
            }
        }

        return $arr;
    }

    // 就诊时间排序
    public static function getWorkDateAsc($arr)
    {
        $count = count($arr);
        if ($count <= 1)
        {
            return $arr;
        }

        $temp = 0;
        for ($i=0; $i < $count-1; $i++)
        {
            for ($j=0; $j < $count-1-$i; $j++)
            {
                if ($arr[$j]->work_date > $arr[$j+1]->work_date)
                {
                    $temp         = $arr[$j];
                    $arr[$j]     = $arr[$j+1];
                    $arr[$j+1]     = $temp;
                }
            }
        }

        return $arr;
    }

    // 获取对象前几位
    public static function getObjectFront($rets, $amount)
    {
        $data = [];
        foreach ($rets as $k => $v)
        {
            if($k < $amount)
            {
                $data[$k] = $v;
            }
        }

        return $data;
    }

    // 获取出诊时间文本
    public static function getVisitText($date, $amPm, $isWeek = true)
    {
        $data = '';
        $dateArr = explode("-",$date);
        if ((int)$dateArr[0] > 0)
        {
            $dateText = (int)$dateArr[1] . '月' . (int)$dateArr[2] . '日';
            if ($isWeek)
            {
                $week = self::getWeek($date);
                // $amPm = self::getAmPm($amPm);
                $data = $dateText . $week;
            }
            else
            {
                $data = $dateText;
            }

        }

        return $data;
    }

    // 上下全天
    public static function getAmPm($amPm)
    {
        switch ($amPm)
        {
        case 1:
          return "上午";
          break;
        case 2:
          return "下午";
          break;
        case 3:
          return "全天";
          break;
        default:
          return "";
        }
    }

    public static function getAmPmTime($date, $amPm)
    {
        switch ($amPm)
        {
        case 1:
          return $date . " 12:00:00";
          break;
        case 2:
          return $date . " 17:00:00";
          break;
        case 3:
          return $date . " 17:00:00";
          break;
        default:
          return "";
        }
    }

    // 初复诊
    public static function getFirstVisit($type)
    {
        if ($type == 0)
        {
            return '复诊';
        }
        else
        {
            return '初诊';
        }
    }

    // tag
    public static function getDisease($type)
    {
        switch ($type) {
            case '0':
                $data = '内科';
                break;
            case '1':
                $data = '外科';
                break;
            case '2':
                $data = '妇科';
                break;
            case '3':
                $data = '儿科';
                break;

            default:
                $data = '';
                break;
        }
        return $data;
    }

    // 获取周几
    public static function getWeek($date)
    {
        //强制转换日期格式
        $date_str=date('Y-m-d',strtotime($date));

        //封装成数组
        $arr=explode("-", $date_str);

        //参数赋值
        //年
        $year=$arr[0];

        //月，输出2位整型，不够2位右对齐
        $month=sprintf('%02d',$arr[1]);

        //日，输出2位整型，不够2位右对齐
        $day=sprintf('%02d',$arr[2]);

        //时分秒默认赋值为0；
        $hour = $minute = $second = 0;

        //转换成时间戳
        $strap = mktime($hour,$minute,$second,$month,$day,$year);

        //获取数字型星期几
        $number_wk=date("w",$strap);

        //自定义星期数组
        $weekArr=array("周日","周一","周二","周三","周四","周五","周六");

        //获取数字对应的星期
        return $weekArr[$number_wk];
    }

    // 获取时间文本
    public static function getDateText($date)
    {
        $data = '';
        $dateArr = explode("-",$date);
        if ((int)$dateArr[0] > 0)
        {
            $data = (int)$dateArr[0] . '年' .(int)$dateArr[1] . '月' . (int)$dateArr[2] . '日';
        }

        return $data;
    }

    // 拆分医生坐诊信息
    public static function getDoctorSchedule($schedule)
    {
        $schedule_list = [];
        foreach ($schedule as $v)
        {
            if($v->mon_am)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周一上午"
                ];
            }

            if($v->mon_pm)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周一下午"
                ];
            }

            if($v->tues_am)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周二上午"
                ];
            }

            if($v->tues_pm)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周二下午"
                ];
            }

            if($v->wed_am)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周三上午"
                ];
            }

            if($v->wed_pm)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周三下午"
                ];
            }
            if($v->thur_am)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周四上午"
                ];
            }

            if($v->thur_pm)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周四下午"
                ];
            }
            if($v->fri_am)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周五上午"
                ];
            }

            if($v->fri_pm)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周五下午"
                ];
            }
            if($v->sat_am)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周六上午"
                ];
            }

            if($v->sat_pm)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周六下午"
                ];
            }
            if($v->sun_am)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周日上午"
                ];
            }

            if($v->sun_pm)
            {
                $schedule_list[] = (object)[
                    'id'    =>    $v->id,
                    'name'    =>    $v->hospital,
                    'time'    =>  "周日下午"
                ];
            }

        }

        return $schedule_list;
    }

    // 获取处理的小数问题
    public static function getDecimal($value = 0)
    {
        if ($value == 0)
        {
            return 0;
        }

        $noArr = explode('.', $value);

        $noDec = intval($noArr[1]);

        if ($noDec > 0)
        {
            $rdata = $noArr[0] . '.' . $noDec;
        }
        else
        {
            $rdata = $noArr[0];
        }

        return $rdata;
    }
}
